import datetime

from django.test import TestCase
from django.test import RequestFactory, TestCase, Client
from django.core.cache import caches

from rest_framework import status

from unittest.mock import MagicMock, patch
from jqgame.basegame import GameType
from jqgame.raffle import quanjushang_raffle
from wxuser.serializers import (
    get_detail_UserPackage,
    calculate_UserPackage_sum_price,
    calculate_UserPackage_sum_price_by_date,
)
from juqiadmin.tests import admin_login
from warehouse.tests import JuqiGoodTest

from juqiadmin.models import JuqiAdmin, get_game_finance

from wxuser.models import Users, UserPackage, UserPackageRecord

from warehouse.models import JuqiGood, GoodLevelGameType, GoodLevel

from wuxianshang.models import (
    WuxianshangGame,
    WuxianshangGameGood,
    WuxianshangRaffleRecord,
    update_price_WuxianshangGame,
)

from warehouse.serializers import JuqiGoodSerializer

from wxuser.tests import UserTest

import json

from pprint import pprint
from .raffle import QuanjushangRaffle

qjs_raffle = QuanjushangRaffle()


# Create your tests here.
# class WuxianshangGameTest(TestCase):
#     def setUp(self):
#         # Every test needs access to the request factory.
#         self.c = Client()
#         self.factory = RequestFactory()
#         self.admin_user = {"user": "admin", "password": "admin123"}
#         u = JuqiAdmin(**self.admin_user)
#         u.save()
#
#     @classmethod
#     def create_a_game(cls) -> WuxianshangGame:
#         from warehouse.models import GoodLevel
#
#         cs_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="传说款"
#         )
#         tx_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="天选款"
#         )
#         bj_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="铂金款"
#         )
#         hj_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="黄金款"
#         )
#         by_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="白银款"
#         )
#
#         a1 = JuqiGood.objects.create(
#             name="A1", display_url="", price=7500, recovery_price=7500, state=1
#         )
#         a2 = JuqiGood.objects.create(
#             name="A2", display_url="", price=6500, recovery_price=6500, state=1
#         )
#         a3 = JuqiGood.objects.create(
#             name="A3", display_url="", price=5500, recovery_price=5500, state=1
#         )
#         a4 = JuqiGood.objects.create(
#             name="A4", display_url="", price=5000, recovery_price=5000, state=1
#         )
#         a5 = JuqiGood.objects.create(
#             name="A5", display_url="", price=4500, recovery_price=4500, state=1
#         )
#         a6 = JuqiGood.objects.create(
#             name="A6", display_url="", price=4000, recovery_price=4000, state=1
#         )
#
#         b1 = JuqiGood.objects.create(
#             name="B1", display_url="", price=1000, recovery_price=1000, state=1
#         )
#         b2 = JuqiGood.objects.create(
#             name="B2", display_url="", price=900, recovery_price=900, state=1
#         )
#         b3 = JuqiGood.objects.create(
#             name="B3", display_url="", price=680, recovery_price=680, state=1
#         )
#         b4 = JuqiGood.objects.create(
#             name="B4", display_url="", price=650, recovery_price=650, state=1
#         )
#         b5 = JuqiGood.objects.create(
#             name="B5", display_url="", price=580, recovery_price=580, state=1
#         )
#         b6 = JuqiGood.objects.create(
#             name="B6", display_url="", price=480, recovery_price=480, state=1
#         )
#
#         c1 = JuqiGood.objects.create(
#             name="C1", display_url="", price=80, recovery_price=80, state=1
#         )
#         c2 = JuqiGood.objects.create(
#             name="C2", display_url="", price=75, recovery_price=75, state=1
#         )
#
#         d1 = JuqiGood.objects.create(
#             name="D1", display_url="", price=30, recovery_price=30, state=1
#         )
#         d2 = JuqiGood.objects.create(
#             name="D2", display_url="", price=5, recovery_price=5, state=1
#         )
#
#         e = JuqiGood.objects.create(
#             name="E", display_url="", price=1, recovery_price=1, state=1
#         )
#
#         wxs_game = WuxianshangGame.objects.create(
#             name="gq_test", display_url="http://zoo", state=1, price=10
#         )
#
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=a1.uuid,
#             good_weight=0.5,
#             level_uuid=cs_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=a2.uuid,
#             good_weight=0.5,
#             level_uuid=cs_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=a3.uuid,
#             good_weight=1,
#             level_uuid=cs_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=a4.uuid,
#             good_weight=1,
#             level_uuid=cs_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=a5.uuid,
#             good_weight=1,
#             level_uuid=cs_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=a6.uuid,
#             good_weight=2,
#             level_uuid=cs_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=b1.uuid,
#             good_weight=3,
#             level_uuid=tx_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=b2.uuid,
#             good_weight=3,
#             level_uuid=tx_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=b3.uuid,
#             good_weight=5,
#             level_uuid=tx_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=b4.uuid,
#             good_weight=5,
#             level_uuid=tx_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=b5.uuid,
#             good_weight=7,
#             level_uuid=tx_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=b6.uuid,
#             good_weight=7,
#             level_uuid=tx_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=c1.uuid,
#             good_weight=100,
#             level_uuid=bj_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=c2.uuid,
#             good_weight=100,
#             level_uuid=bj_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=d1.uuid,
#             good_weight=100,
#             level_uuid=hj_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=d2.uuid,
#             good_weight=800,
#             level_uuid=hj_level.uuid,
#         )
#         WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=e.uuid,
#             good_weight=8864,
#             level_uuid=by_level.uuid,
#         )
#
#         good_uuid = []
#         for v in WuxianshangGameGood.objects.filter(game_uuid=wxs_game.uuid):
#             good_uuid.append(v.good_uuid)
#
#         goods = JuqiGood.objects.filter(uuid__in=good_uuid)
#
#         return wxs_game, goods
#
#     # python .\manage.py test wuxianshang.tests.WuxianshangGameTest.test_query_single_game
#     # def test_query_single_game(self):
#     #     game, _ = WuxianshangGameTest.create_a_game()
#     #     # login
#     #     r = self.c.post(
#     #         "/api/admin/login",
#     #         self.admin_user,
#     #         content_type="application/json",
#     #     )
#     #     self.assertEqual(r.status_code, status.HTTP_200_OK)
#     #
#     #     d = json.loads(r.content)
#     #     self.assertEqual(len(self.c.session.get("login_token", "")), 64)
#     #     self.assertEqual(d["code"], "0")
#     #
#     #     # get single good
#     #     r = self.c.get(
#     #         "/api/wuxianshang/game",
#     #         {"name": game.name},
#     #         content_type="application/json",
#     #     )
#     #     self.assertEqual(r.status_code, status.HTTP_200_OK)
#     #
#     #     res = json.loads(r.content)
#     #     self.assertEqual(res["name"], game.name)
#     #     self.assertEqual(res["display_url"], game.display_url)
#     #     self.assertEqual(res["price"], 300)
#
#     # python .\manage.py test wuxianshang.tests.WuxianshangGameTest.test_query_many_game
#     # def test_query_many_game(self):
#     #     game, goods = WuxianshangGameTest.create_a_game()
#     #     # login
#     #     r = self.c.post(
#     #         "/api/admin/login",
#     #         self.admin_user,
#     #         content_type="application/json",
#     #     )
#     #     self.assertEqual(r.status_code, status.HTTP_200_OK)
#     #
#     #     d = json.loads(r.content)
#     #     self.assertEqual(len(self.c.session.get("login_token", "")), 64)
#     #     self.assertEqual(d["code"], "0")
#     #
#     #     # get single good
#     #     r = self.c.get(
#     #         "/api/wuxianshang/game",
#     #         {"name": game.name, "list": 1, "page": 1, "num": 10},
#     #         content_type="application/json",
#     #     )
#     #     self.assertEqual(r.status_code, status.HTTP_200_OK)
#     #
#     #     res = json.loads(r.content)
#     #     self.assertEqual(res[0]["name"], game.name)
#     #     self.assertEqual(res[0]["display_url"], game.display_url)
#     #     self.assertEqual(res[0]["price"], 1000)
#
#     # python .\manage.py test wuxianshang.tests.WuxianshangGameTest.test_post_game
#     def test_post_game(self):
#         # login
#         r = self.c.post(
#             "/api/admin/login",
#             self.admin_user,
#             content_type="application/json",
#         )
#         self.assertEqual(r.status_code, status.HTTP_200_OK)
#
#         d = json.loads(r.content)
#         self.assertEqual(len(self.c.session.get("login_token", "")), 64)
#         self.assertEqual(d["code"], "0")
#
#         # create good
#         goods = [
#             {
#                 "name": "bee",
#                 "display_url": "http://bee",
#                 "price": 100,
#                 "game_type": "wuxianshang",
#             },
#             {
#                 "name": "duck",
#                 "display_url": "http://duck",
#                 "price": 9900,
#                 "game_type": "wuxianshang",
#             },
#         ]
#
#         weights = {
#             "bee": 99,
#             "duck": 1,
#         }
#
#         rarities = {
#             "bee": "白银款",
#             "duck": "黄金款",
#         }
#
#         goods_info = []
#         for g in goods:
#             r = self.c.post(
#                 "/api/warehouse/good",
#                 g,
#                 content_type="application/json",
#             )
#             self.assertEqual(r.status_code, status.HTTP_201_CREATED)
#             good = json.loads(r.content)
#             weight = weights[good["name"]]
#             rarity = rarities[good["name"]]
#             goods_info.append(
#                 {
#                     "good_uuid": good["uuid"],
#                     "good_weight": weight,
#                     "good_rarity": rarity,
#                 }
#             )
#
#         # create game
#         game = {"name": "zoo", "display_url": "http://zoo"}
#         game["goods"] = goods_info
#         r = self.c.post(
#             "/api/wuxianshang/game",
#             game,
#             content_type="application/json",
#         )
#         self.assertEqual(r.status_code, status.HTTP_201_CREATED)
#
#         res = json.loads(r.content)
#         self.assertEqual(res["name"], game["name"])
#         self.assertEqual(res["display_url"], game["display_url"])
#         self.assertEqual(res["price"], 999999)


# python .\manage.py test wuxianshang.tests.WuxianshangRaffleTest
# class WuxianshangRaffleTest(TestCase):
#     def setUp(self):
#         # Every test needs access to the request factory.
#         self.wx_c = Client(HTTP_X_WX_OPENID="wx123")
#
#     def test_raffle_bee_use_stored_money(self):
#         _ = UserTest.create_a_user()
#         game, goods = WuxianshangGameTest.create_a_game()
#         bee = JuqiGood.objects.get(name="bee")
#
#         # raffle
#         raffle_count = 3
#
#         with patch("wuxianshang.views.wxs_raffle") as _wxs_raffle:
#             _wxs_raffle.return_value = {bee.uuid: raffle_count}
#             r = self.wx_c.post(
#                 "/api/wuxianshang/raffle/",
#                 {
#                     "pay_type": "stored_money",
#                     "raffle_count": raffle_count,
#                     "game_uuid": game.uuid,
#                 },
#                 content_type="application/json",
#             )
#             d = json.loads(r.content)
#
#             self.assertEqual(r.status_code, status.HTTP_200_OK)
#
#             self.assertEqual(len(d["goods"]), raffle_count)
#
#             admin_finance = get_game_finance(GameType.wuxianshang)
#
#             # frash game price
#             _game = WuxianshangGame.objects.get(uuid=game.uuid)
#             self.assertEqual(
#                 admin_finance.all_raffle_income, _game.price * raffle_count
#             )
#             user = Users.objects.get(openid="wx123")
#
#             user_package = UserPackage.objects.filter(user_uuid=user.uuid).first()
#             self.assertEqual(user_package.user_uuid, user.uuid)
#             self.assertEqual(user_package.good_uuid, bee.uuid)
#             self.assertEqual(user_package.count, raffle_count)
#
#             wuxianshangRaffleRecord = WuxianshangRaffleRecord.objects.filter(
#                 user_uuid=user.uuid
#             ).first()
#
#             self.assertEqual(wuxianshangRaffleRecord.count, raffle_count)
#             self.assertEqual(wuxianshangRaffleRecord.game_uuid, game.uuid)
#             self.assertEqual(wuxianshangRaffleRecord.user_uuid, user.uuid)
#             self.assertEqual(wuxianshangRaffleRecord.good_uuid, bee.uuid)
#
#             r = self.wx_c.get(
#                 "/api/wuxianshang/raffle/",
#                 {"list": "1", "page": 1, "num": 50, "game_uuid": game.uuid},
#                 content_type="application/json",
#             )
#             d = json.loads(r.content)
#             self.assertEqual(d[0]["game"]["count"], raffle_count)
#             self.assertEqual(d[0]["good"]["count"], raffle_count)
#             self.assertEqual(d[0]["good"]["uuid"], str(bee.uuid))
#
#             # test exchange hunjing
#             old_hunjing = user.hunjing
#
#             r = self.wx_c.delete(
#                 "/api/wxuser/userpackage",
#                 {
#                     "good_uuid": bee.uuid,
#                     "count": 2,
#                 },
#                 content_type="application/json",
#             )
#             d = json.loads(r.content)
#             self.assertEqual(d["user"]["hunjing"], old_hunjing + 2 * bee.price)
#             self.assertEqual(d["goods"][0]["count"], raffle_count - 2)
#
#     def test_raffle_duck_use_stored_money(self):
#         _ = UserTest.create_a_user()
#         game, goods = WuxianshangGameTest.create_a_game()
#         duck = JuqiGood.objects.get(name="duck")
#
#         # raffle
#         raffle_count = 3
#
#         with patch("wuxianshang.views.wxs_raffle") as _wxs_raffle:
#             _wxs_raffle.return_value = {duck.uuid: raffle_count}
#             r = self.wx_c.post(
#                 "/api/wuxianshang/raffle/",
#                 {
#                     "pay_type": "stored_money",
#                     "raffle_count": raffle_count,
#                     "game_uuid": game.uuid,
#                 },
#                 content_type="application/json",
#             )
#             d = json.loads(r.content)
#
#             self.assertEqual(r.status_code, status.HTTP_400_BAD_REQUEST)
#             self.assertEqual(d["msg"], "server is busy, please retry raffle")
#
#             user = Users.objects.get(openid="wx123")
#             user_package = UserPackage.objects.filter(user_uuid=user.uuid).exists()
#
#             self.assertEqual(user_package, False)


# python .\manage.py test wuxianshang.tests.WuxianshangArgsTest
# class WuxianshangArgsTest(TestCase):
#     def setUp(self):
#         # Every test needs access to the request factory.
#         self.c = Client()
#         self.admin_user = {"user": "admin", "password": "admin123"}
#         u = JuqiAdmin(**self.admin_user)
#         u.save()
#
#     def test_query_and_change_WuxianshangArgs(self):
#         #  test err right
#         r = self.c.get(
#             "/api/wuxianshang/wuxianshangargs",
#             content_type="application/json",
#         )
#         self.assertEqual(r.status_code, status.HTTP_400_BAD_REQUEST)
#
#         # login
#         r = self.c.post(
#             "/api/admin/login",
#             self.admin_user,
#             content_type="application/json",
#         )
#         self.assertEqual(r.status_code, status.HTTP_200_OK)
#
#         d = json.loads(r.content)
#         self.assertEqual(len(self.c.session.get("login_token", "")), 64)
#         self.assertEqual(d["code"], "0")
#
#         # query after login
#         r = self.c.get(
#             "/api/wuxianshang/wuxianshangargs",
#             content_type="application/json",
#         )
#         self.assertEqual(r.status_code, status.HTTP_200_OK)
#
#         d = json.loads(r.content)
#         self.assertEqual(float(d["wxs_min_sale_profit_percent"]), 0.1)
#         self.assertEqual(d["wxs_min_sale_profit_window"], 30)
#         self.assertEqual(d["wxs_user_lucky_window"], 30)
#
#         params = {
#             "wxs_min_sale_profit_percent": 0.11,
#             "wxs_min_sale_profit_window": 31,
#             "wxs_user_lucky_window": 32,
#         }
#         r = self.c.put(
#             "/api/wuxianshang/wuxianshangargs",
#             params,
#             content_type="application/json",
#         )
#         self.assertEqual(r.status_code, status.HTTP_200_OK)
#
#         d = json.loads(r.content)
#         self.assertEqual(float(d["wxs_min_sale_profit_percent"]), 0.11)
#         self.assertEqual(d["wxs_min_sale_profit_window"], 31)
#         self.assertEqual(d["wxs_user_lucky_window"], 32)


# class WuxianshangExtendBossRaffleTest(TestCase):
#     def setUp(self):
#         self.wx_c = Client(HTTP_X_WX_OPENID="wx123")
#
#         self.admin_c = Client()
#
#     # python .\manage.py test wuxianshang.tests.WuxianshangExtendBossRaffleTest.test_create_extendBoss_and_raffle
#     def test_create_extendBoss_and_raffle(self):
#         admin_login(self.admin_c)
#
#         # 创建 boss 赏商品
#         goods = [
#             {
#                 "name": "wxs_good_1",
#                 "display_url": "img",
#                 "price": 10e2,
#                 "game_type": "无限赏",
#                 "ip_family": "一拳超人",
#             },
#             {
#                 "name": "wxs_good_2",
#                 "display_url": "img",
#                 "price": 100e2,
#                 "game_type": "无限赏",
#                 "ip_family": "一拳超人",
#             },
#             {
#                 "name": "wxs_good_3",
#                 "display_url": "img",
#                 "price": 1000e2,
#                 "game_type": "无限赏",
#                 "ip_family": "一拳超人",
#             },
#             {
#                 "name": "global_boss_good_1",
#                 "display_url": "img",
#                 "price": 10e2,
#                 "game_type": "boss赏",
#                 "ip_family": "一拳超人",
#             },
#             {
#                 "name": "global_boss_good_2",
#                 "display_url": "img",
#                 "price": 20e2,
#                 "game_type": "boss赏",
#                 "ip_family": "一拳超人",
#             },
#             {
#                 "name": "global_boss_good_3",
#                 "display_url": "img",
#                 "price": 30e2,
#                 "game_type": "boss赏",
#                 "ip_family": "一拳超人",
#             },
#             {
#                 "name": "each_boss_good_1",
#                 "display_url": "img",
#                 "price": 1e2,
#                 "game_type": "boss赏",
#                 "ip_family": "一拳超人",
#             },
#         ]
#         good_name_map = {}
#         wxs_goods = []
#         boss_goods = []
#         for good in goods:
#             r = JuqiGoodTest.create_good(client=self.admin_c, good_info=good)
#             self.assertEqual(r.status_code, status.HTTP_201_CREATED)
#
#             d = json.loads(r.content)
#             good_name_map[good["name"]] = d
#             if d["game_type"] == "无限赏":
#                 wxs_goods.append(
#                     {"good_uuid": d["uuid"], "good_weight": 1, "good_rarity": "传说款"}
#                 )
#             elif d["game_type"] == "boss赏":
#                 is_global = int(str(d["name"]).startswith("global_"))
#                 boss_goods.append(
#                     {
#                         "good_uuid": d["uuid"],
#                         "is_global": is_global,
#                         "good_rarity": "传说款",
#                         "count": 10,
#                     }
#                 )
#
#         game = {
#             "name": "wxs_enable_boss",
#             "display_url": "http://",
#             "goods": wxs_goods,
#             "extend_type_boss": True,
#             "state": 1,
#             "extend_boss": {
#                 "name": "boss赏1",
#                 "ticket_weight": 1,
#                 "draw_num": 100,
#                 "display_url": "img",
#                 "goods": boss_goods,
#             },
#         }
#
#         # 创建 无限赏 盒子
#         r = self.admin_c.post(
#             "/api/wuxianshang/game",
#             game,
#             content_type="application/json",
#         )
#         self.assertEqual(r.status_code, status.HTTP_201_CREATED)
#
#         _game = json.loads(r.content)
#
#         self.assertEqual(_game["name"], game["name"])
#         self.assertEqual(_game["display_url"], game["display_url"])
#
#         # pprint(_game)
#         # _extend_boss = _game["extend_boss"]
#         # _ticket_uuid = _extend_boss["ticket_uuid"]
#         # # boss 抽奖, 全部返回奖票
#         # _user = UserTest.create_a_user()
#         # with patch("wuxianshang.views.wxs_raffle") as _wxs_raffle:
#         #     raffle_count = 101
#         #     _wxs_raffle.return_value = {_ticket_uuid: raffle_count}
#         #     params = {
#         #         "raffle_count": raffle_count,
#         #         "pay_type": "stored_money",
#         #         "game_uuid": _game["uuid"],
#         #     }
#         #     r = self.wx_c.post(
#         #         "/api/wuxianshang/raffle",
#         #         params,
#         #         content_type="application/json",
#         #     )
#         #     raffle_res = json.loads(r.content)
#         #     # pprint(raffle_res)
#         #     for u in Users.objects.filter(deleted=False):
#         #         up = get_detail_UserPackage(u)
#         #         pprint("用户背包")
#         #         pprint(up)
#         #         all_sum = calculate_UserPackage_sum_price(u.uuid)
#         #         pprint("赏柜总价")
#         #         pprint(all_sum)
#         #         current_date_sum = calculate_UserPackage_sum_price_by_date(
#         #             u.uuid, datetime.datetime.now()
#         #         )
#         #         pprint("今天的赏柜总价")
#         #         pprint(current_date_sum)
#
#     # python .\manage.py test wuxianshang.tests.WuxianshangExtendBossRaffleTest.test_global_prize_raffle
#     def test_global_prize_raffle(self):
#         user_weight = {
#             "zhangsan": 1,
#             "xiaoliang": 4,
#             "lisi": 2,
#         }
#         goods = {
#             "bee": 12,
#             "cat": 8,
#             "dog": 4,
#             "duck": 1,
#         }
#         good_count = sum(goods.values())
#
#         prize_num = int(1e3)
#         res = {g: {} for g in goods}
#         for _ in range(prize_num):
#             _once_good_count = 0
#             _res = quanjushang_raffle(user_weight=user_weight, boss_goods_count=goods)
#             for k in _res:
#                 for user_uuid in _res[k]:
#                     _once_count = _res[k][user_uuid]
#                     _once_good_count += _once_count
#                     if user_uuid not in res[k]:
#                         res[k][user_uuid] = _once_count
#                     else:
#                         res[k][user_uuid] += _once_count
#             self.assertEqual(_once_good_count, good_count)


class NewWxsRaffleTest(TestCase):
    def setUp(self):
        self.c = Client(HTTP_X_WX_OPENID="wx123")
        self.factory = RequestFactory()

    @classmethod
    def create_a_user(cls):
        d = {
            "nickname": "xiaoming",
            "head_url": "http://nick",
            "openid": "wx123",
            "stored_money": 1000000,
            "hunjing": 100000,
            "score": 0,
        }
        u = Users(**d)
        u.save()
        return u

    def test_new_raffle(self):
        NewWxsRaffleTest.create_a_user()
        from warehouse.models import GoodLevel

        cs_level = GoodLevel.objects.create(
            game_type=GoodLevelGameType.wuxianshang.value, title="传说款"
        )
        tx_level = GoodLevel.objects.create(
            game_type=GoodLevelGameType.wuxianshang.value, title="钻石款"
        )
        bj_level = GoodLevel.objects.create(
            game_type=GoodLevelGameType.wuxianshang.value, title="铂金款"
        )
        hj_level = GoodLevel.objects.create(
            game_type=GoodLevelGameType.wuxianshang.value, title="黄金款"
        )
        by_level = GoodLevel.objects.create(
            game_type=GoodLevelGameType.wuxianshang.value, title="白银款"
        )

        a1 = JuqiGood.objects.create(
            name="A1", display_url="", price=7500, recovery_price=7500, state=1
        )
        a2 = JuqiGood.objects.create(
            name="A2", display_url="", price=6500, recovery_price=6500, state=1
        )
        a3 = JuqiGood.objects.create(
            name="A3", display_url="", price=5500, recovery_price=5500, state=1
        )
        a4 = JuqiGood.objects.create(
            name="A4", display_url="", price=5000, recovery_price=5000, state=1
        )
        a5 = JuqiGood.objects.create(
            name="A5", display_url="", price=4500, recovery_price=4500, state=1
        )
        a6 = JuqiGood.objects.create(
            name="A6", display_url="", price=4000, recovery_price=4000, state=1
        )

        b1 = JuqiGood.objects.create(
            name="B1", display_url="", price=1000, recovery_price=1000, state=1
        )
        b2 = JuqiGood.objects.create(
            name="B2", display_url="", price=900, recovery_price=900, state=1
        )
        b3 = JuqiGood.objects.create(
            name="B3", display_url="", price=680, recovery_price=680, state=1
        )
        b4 = JuqiGood.objects.create(
            name="B4", display_url="", price=650, recovery_price=650, state=1
        )
        b5 = JuqiGood.objects.create(
            name="B5", display_url="", price=580, recovery_price=580, state=1
        )
        b6 = JuqiGood.objects.create(
            name="B6", display_url="", price=480, recovery_price=480, state=1
        )

        c1 = JuqiGood.objects.create(
            name="C1", display_url="", price=80, recovery_price=80, state=1
        )
        c2 = JuqiGood.objects.create(
            name="C2", display_url="", price=75, recovery_price=75, state=1
        )

        d1 = JuqiGood.objects.create(
            name="D1", display_url="", price=30, recovery_price=30, state=1
        )
        d2 = JuqiGood.objects.create(
            name="D2", display_url="", price=5, recovery_price=5, state=1
        )

        e = JuqiGood.objects.create(
            name="E", display_url="", price=1, recovery_price=1, state=1
        )

        wxs_game = WuxianshangGame.objects.create(
            name="gq_test", display_url="http://zoo", state=1, price=10
        )

        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=a1.uuid,
            good_weight=0.5,
            level_uuid=cs_level.uuid,
            sort=1,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=a2.uuid,
            good_weight=0.5,
            level_uuid=cs_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=a3.uuid,
            good_weight=1,
            level_uuid=cs_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=a4.uuid,
            good_weight=1,
            level_uuid=cs_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=a5.uuid,
            good_weight=1,
            level_uuid=cs_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=a6.uuid,
            good_weight=2,
            level_uuid=cs_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=b1.uuid,
            good_weight=3,
            level_uuid=tx_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=b2.uuid,
            good_weight=3,
            level_uuid=tx_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=b3.uuid,
            good_weight=5,
            level_uuid=tx_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=b4.uuid,
            good_weight=5,
            level_uuid=tx_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=b5.uuid,
            good_weight=7,
            level_uuid=tx_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=b6.uuid,
            good_weight=7,
            level_uuid=tx_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=c1.uuid,
            good_weight=100,
            level_uuid=bj_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=c2.uuid,
            good_weight=100,
            level_uuid=bj_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=d1.uuid,
            good_weight=100,
            level_uuid=hj_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=d2.uuid,
            good_weight=800,
            level_uuid=hj_level.uuid,
        )
        WuxianshangGameGood.objects.create(
            game_uuid=wxs_game.uuid,
            good_uuid=e.uuid,
            good_weight=8864,
            level_uuid=by_level.uuid,
        )

        wxs_game_uuid = str(wxs_game.uuid)
        qjs_raffle.init_prize(wxs_game_uuid)

        raffle = self.c.post(
            "/api/wuxianshang/raffle/",
            {
                "stored_money": 1,
                "raffle_count": 10,
                "game_uuid": wxs_game.uuid,
            },
            content_type="application/json",
        )

        raffle_j = json.loads(raffle.content)
        print(raffle_j, "guoqing111")
        # print(qjs_raffle.qjs_raffle(152, wxs_game_uuid, 20), "gq")
        # print(qjs_raffle.qjs_raffle(151, wxs_game_uuid, 20), "gq")
        # print(qjs_raffle.qjs_raffle(152, wxs_game_uuid, 10), "gq")


class TestGameDetailView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

    def test_get_game_detail(self):
        self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )

        game = WuxianshangGame.objects.create(name="")

        game_detail = self.c.get(
            "/api/wuxianshang/game/detail",
            {
                "uuid": game.uuid,
            },
        )

        game_detail_s = json.loads(game_detail.content)
        self.assertEqual(game_detail_s["code"], 0)


# class TestGameGoodView(TestCase):
#     def setUp(self):
#         # Every test needs access to the request factory.
#         self.c = Client()
#         self.admin_user = {"user": "admin", "password": "admin123"}
#         u = JuqiAdmin(**self.admin_user)
#         u.save()
#
#     def test_get_game_good_detail(self):
#         self.c.post(
#             "/api/admin/login",
#             self.admin_user,
#             content_type="application/json",
#         )
#
#         e = JuqiGood.objects.create(
#             name="E", display_url="", price=1, recovery_price=1, state=1
#         )
#         wxs_game = WuxianshangGame.objects.create(
#             name="gq_test", display_url="http://zoo", state=1, price=10
#         )
#         cs_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="传说款"
#         )
#
#         game_good = WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=e.uuid,
#             good_weight=0.5,
#             level_uuid=cs_level.uuid,
#         )
#
#         detail = self.c.get(
#             "/api/wuxianshang/gamegood/detail",
#             {
#                 "uuid": game_good.uuid,
#             },
#             content_type="application/json",
#         )
#
#         detail_j = json.loads(detail.content)
#         self.assertEqual(detail_j["code"], 0)
#
#     def test_put_game_good(self):
#         self.c.post(
#             "/api/admin/login",
#             self.admin_user,
#             content_type="application/json",
#         )
#
#         e = JuqiGood.objects.create(
#             name="E", display_url="", price=1, recovery_price=1, state=1
#         )
#         wxs_game = WuxianshangGame.objects.create(
#             name="gq_test", display_url="http://zoo", price=10
#         )
#         cs_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="传说款"
#         )
#         tx_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="天选款"
#         )
#
#         game_good = WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=e.uuid,
#             good_weight=0.5,
#             level_uuid=cs_level.uuid,
#         )
#
#         update_game_good = self.c.put(
#             "/api/wuxianshang/gamegood",
#             {
#                 "uuid": game_good.uuid,
#                 "good_weight": 1,
#                 "level_uuid": tx_level.uuid,
#             },
#             content_type="application/json",
#         )
#
#         update_game_good_ = json.loads(update_game_good.content)
#         print(update_game_good_, "guoqing1111")
#
#     def test_delete_game_good(self):
#         self.c.post(
#             "/api/admin/login",
#             self.admin_user,
#             content_type="application/json",
#         )
#
#         e = JuqiGood.objects.create(
#             name="E", display_url="", price=1, recovery_price=1, state=1
#         )
#         wxs_game = WuxianshangGame.objects.create(
#             name="gq_test", display_url="http://zoo", price=10
#         )
#         cs_level = GoodLevel.objects.create(
#             game_type=GoodLevelGameType.wuxianshang.value, title="传说款"
#         )
#
#         game_good = WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game.uuid,
#             good_uuid=e.uuid,
#             good_weight=0.5,
#             level_uuid=cs_level.uuid,
#         )
#
#         # 删除未上架池子的赏品
#         delete_game_good = self.c.delete(
#             "/api/wuxianshang/gamegood",
#             {
#                 "uuid": game_good.uuid,
#             },
#             content_type="application/json",
#         )
#         delete_game_good_ = json.loads(delete_game_good.content)
#         self.assertEqual(delete_game_good_["code"], 0)
#
#         # 删除已上架池子的赏品
#         wxs_game2 = WuxianshangGame.objects.create(
#             name="gq_test2", display_url="http://zoo", state=1, price=10
#         )
#         game_good2 = WuxianshangGameGood.objects.create(
#             game_uuid=wxs_game2.uuid,
#             good_uuid=e.uuid,
#             good_weight=0.5,
#             level_uuid=cs_level.uuid,
#         )
#         delete_online_game_good = self.c.delete(
#             "/api/wuxianshang/gamegood",
#             {
#                 "uuid": game_good2.uuid,
#             },
#             content_type="application/json",
#         )
#         delete_online_game_good_ = json.loads(delete_online_game_good.content)
#         self.assertEqual(delete_online_game_good_["code"], -1)
